package cn.edu.njuit.springbootfilmreview.service.impl;

import cn.edu.njuit.springbootfilmreview.dto.CommentInfo;
import cn.edu.njuit.springbootfilmreview.dto.WantInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.njuit.springbootfilmreview.dto.WorksInfo;
import cn.edu.njuit.springbootfilmreview.service.WorksInfoService;
import cn.edu.njuit.springbootfilmreview.mapper.WorksInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author Administrator
 * @description 针对表【works_info(发布作品的信息表)】的数据库操作Service实现
 * @createDate 2022-05-23 19:35:25
 */
@Service
public class WorksInfoServiceImpl extends ServiceImpl<WorksInfoMapper, WorksInfo>
        implements WorksInfoService {

    @Autowired
    WorksInfoMapper mapper;

    @Override
    public IPage<WorksInfo> getPageAllWorksInfo(Integer currentPage, int pageSize) {
        IPage page = new Page(currentPage, pageSize);
        mapper.selectPage(page, null);
        return page;
    }

    @Override
    public boolean addWorkInfo(WorksInfo worksInfo) {
        int insert = mapper.insert(worksInfo);
        return insert > 0;
    }

    @Override
    public boolean updateWorkInfo(WorksInfo worksInfo) {
        int i = mapper.updateById(worksInfo);
        return i > 0;
    }

    @Override
    public boolean deleteWorkInfoById(Long id) {
        LambdaQueryWrapper<WorksInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WorksInfo::getId, id);
        int delete = mapper.delete(lqw);
        return delete > 0;
    }

    @Override
    public IPage<WorksInfo> getWorkInfoListByUserId(Integer currentPage, int pageSize, Long userId) {
        LambdaQueryWrapper<WorksInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WorksInfo::getUserId, userId);
        IPage page = new Page(currentPage, pageSize);
        mapper.selectPage(page, lqw);
        return page;
    }

    @Override
    public Optional<WorksInfo> getWorkInfoById(Long id) {
        LambdaQueryWrapper<WorksInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WorksInfo::getId, id);
        WorksInfo worksInfo = mapper.selectOne(lqw);
        return Optional.ofNullable(worksInfo);
    }
}
